package com.lzh.chatnote.chat.service.strategy;

import cn.hutool.core.collection.CollUtil;
import com.lzh.chatnote.chat.dao.MessageDao;
import com.lzh.chatnote.chat.domain.dto.message.MsgTextDTO;
import com.lzh.chatnote.chat.domain.entity.Message;
import com.lzh.chatnote.chat.domain.entity.MessageExtra;
import com.lzh.chatnote.chat.domain.enums.MessageStatusEnum;
import com.lzh.chatnote.chat.domain.enums.MessageTypeEnum;
import com.lzh.chatnote.chat.domain.vo.response.message.TextMsgResp;
import com.lzh.chatnote.chat.service.cache.MessageCache;
import com.lzh.chatnote.user.domain.entity.User;
import com.lzh.chatnote.user.service.cache.UserInfoCache;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;


/**
 * 文本消息处理器
 */

@Component
public class TextMsgHandler extends AbstractMsgHandler<MsgTextDTO>{

    @Resource
    private MessageDao messageDao;
    @Resource
    private MessageCache messageCache;
    @Resource
    private UserInfoCache userInfoCache;

    @Override
    protected MessageTypeEnum getMessageTypeEnum() {
        return MessageTypeEnum.TEXT;
    }

    //保存文本消息的具体逻辑
    @Override
    protected void saveMessage(Message message, MsgTextDTO req) {
        MessageExtra extra = Optional.ofNullable(message.getExtra()).orElse(new MessageExtra());
        Message updateMsg = new Message();
        //todo过滤敏感词
        updateMsg.setContent(req.getContent());
        updateMsg.setExtra(extra);
        updateMsg.setId(message.getId());
        //保存扩展数据
        updateMsg.setExtra(extra);
        //回复消息
        if(Objects.nonNull(req.getReplyMsgId())){
           Integer gapCount = messageDao.getGapCount(message.getRoomId(),message.getId(),req.getReplyMsgId());
           updateMsg.setGapCount(gapCount);
           updateMsg.setReplyMsgId(req.getReplyMsgId());
        }
        //保存解析发送消息里面是否存在链接todo

        //被at人员数据保存
        if(CollUtil.isNotEmpty(req.getAtUserIds())){
            extra.setAtUids(req.getAtUserIds());
        }
        //修改数据
        messageDao.updateById(updateMsg);
        //删除消息缓存
        messageCache.evictMsg(message.getId());
    }

    @Override
    public Object showMsgJsonObj(Message message) {
        TextMsgResp textMsgResp = new TextMsgResp();
        //消息内容
        textMsgResp.setContent(message.getContent());
        //被@人员
        textMsgResp.setAtUidList(message.getExtra().getAtUids());
        //回复消息
        Optional<Message> replyMsg = Optional.ofNullable(message.getReplyMsgId())
                .map(messageCache::getMsg)
                .filter(msg -> msg.getStatus().equals(MessageStatusEnum.NORMAL.getStatus()));
        if(replyMsg.isPresent()){
            //回复消息
            Message replyMessage = replyMsg.get();
            //构建对象
            TextMsgResp.ReplyMsg reply = new TextMsgResp.ReplyMsg();
            //消息id
            reply.setId(replyMessage.getId());
            //用户
            reply.setUid(replyMessage.getFromUid());
            //类型
            reply.setType(MessageTypeEnum.of(replyMessage.getType()).getType());
            //用户名称
            User userInfo = userInfoCache.getOne(replyMessage.getFromUid());
            reply.setUsername(userInfo.getUsername());
            //间隔条数
            reply.setGapCount(message.getGapCount());
            //消息是否可以跳转todo

            //消息内容(根据工厂获取对应的工厂运行对象是啥)
            reply.setBody(MsgFactoryHandler.getStrategyNoNull(replyMessage.getType()).showReplyMsgJsonObj(replyMessage));

            textMsgResp.setReply(reply);
        }
        return textMsgResp;
    }

    @Override
    public Object showReplyMsgJsonObj(Message message) {
        return message.getContent();
    }


}
